package com.demo.rabbitmq.returnd;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author sc
 * Created on  2018/9/16
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setVirtualHost("/");
        cf.setHost("localhost");
        cf.setPort(5672);
        cf.setNetworkRecoveryInterval(3000);
        cf.setAutomaticRecoveryEnabled(true);

        Connection connection = cf.newConnection();
        Channel channel = connection.createChannel();
        channel.confirmSelect();
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("reply code"+replyCode);
                System.out.println("reply text"+replyText);
                System.out.println("exchange"+exchange);
                System.out.println("routingKey"+routingKey);
                System.out.println("properties"+properties);
                System.out.println("body"+new String(body));
            }
        });

        String exchange = "test_return";
        String routingKey = "return.a";

        channel.basicPublish(exchange,routingKey,true,null,"confirm".getBytes());

        Thread.sleep(10000000);
    }
}
